package com.activities.api.data.providers;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.activities.api.data.entities.Group;

public class GroupStoreImpl extends StoreImpl<Group> implements GroupStore {

	@Override
	public Group get(String id) {
		EntityManager manager = getManager();
		Group result;
		try {
			result = manager.find(Group.class, id);
		} finally {
			manager.close();
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Group> getAll() {
		EntityManager manager = getManager();
		try {
			Query query = manager.createQuery(String.format("SELECT x FROM %s x", Group.class.getName()));
			Collection<Group> result = query.getResultList();
			result.size();
			
			return result;
		} finally {
			manager.close();
		}
	}

	@Override
	public void save(Group item) {
		EntityManager manager = getManager();
		try {
//			for (Participant p : item.getParticipants()) {
//				System.out.println(p.getUser().getEmail());
//			}
			manager.persist(item);
		} finally {
			manager.close();
		}
	}

	@Override
	public void delete(String id) {
		EntityManager manager = getManager();

		try {
			Group item = manager.find(Group.class, id);

			manager.remove(item);
		} finally {
			manager.close();
		}
	}
}
